//
//  BKCusImgAndLabbtn.m
//  bestkeep
//
//  Created by windCould on 16/11/7.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKCusImgAndLabbtn.h"
#import "BKMyCommon.h"

@interface BKCusImgAndLabbtn ()

@property (nonatomic,strong)UIImageView * imgV;
@property (nonatomic,strong)UILabel * datailLabel;

@end

@implementation BKCusImgAndLabbtn


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpViews];
        
    }
    
    return  self;
}

-(void)setUpViews{
    
    _imgV = [[UIImageView alloc]initWithFrame:CGRectZero];
    _imgV.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_imgV];
    _bageLabel = [[UILabel alloc]init];
    _bageLabel.textColor = [UIColor whiteColor];
    _bageLabel.backgroundColor=[UIColor redColor];
    _bageLabel.textAlignment = NSTextAlignmentCenter;
    _bageLabel.font = [UIFont systemFontOfSize:9];
    [_imgV addSubview:_bageLabel];
    
    _datailLabel = [UILabel new];
    _datailLabel.textColor = BK_MY_COLOR_04;
    _datailLabel.font = [UIFont systemFontOfSize:14.0];
    _datailLabel.textAlignment = NSTextAlignmentCenter;
    _datailLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:_datailLabel];
    [self layoutViews];
    
}

-(void)layoutViews{
    
    [_imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.center.x).offset(0);
        make.top.mas_equalTo(self.mas_top).offset(5);
        make.size.mas_equalTo(CGSizeMake(26, 26));
    }];
    [_bageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.imgV.mas_right).offset(-8);
        make.height.mas_equalTo(0);
        make.width.mas_equalTo(0);
        
    }];
    
    [_datailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.center.x).offset(0);
        make.top.mas_equalTo(self.imgV.mas_bottom).offset(1);
        make.width.mas_equalTo(self.bounds.size.width);
        make.height.mas_equalTo(@(20));
    }];
    
    
}

-(void)setImgNameStr:(NSString *)imgNameStr
{
    UIImage * img = [UIImage imageNamed:imgNameStr];
    _imgV.image = img;
}

-(void)setItemTitle:(NSString *)itemTitle
{
    _datailLabel.text = itemTitle;
}
-(void)setBageStr:(NSString *)bageStr{
    self.width = [self getWidthWithTitle:@"99" font:14];
    if ([bageStr isEqualToString:@"0"]||bageStr.length==0) {
        _bageLabel.text = @"";
        _bageLabel.size = CGSizeMake(0, 0);
    }
    
    if ([bageStr integerValue]>99) {
        _bageLabel.text = @"99+";
        _bageLabel.size = CGSizeMake(self.width+2, 15);
        _bageLabel.layer.cornerRadius=15.0f/2;
        _bageLabel.clipsToBounds = YES;
    }
    if ([bageStr integerValue]>0 &&[bageStr integerValue]<99) {
        _bageLabel.text = bageStr;
        _bageLabel.size = CGSizeMake(15, 15);
        _bageLabel.layer.cornerRadius=15.0f/2;
        _bageLabel.clipsToBounds = YES;
    }
    
}
-(CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
    
}

@end
